﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using backoffice.Data;
using System.Linq.Expressions;

namespace backoffice.Business
{
    public class BusinessExpert
    {
        #region Create
        public static void createExpert(Expert e)
        {
            try
            {
                dataExpert.createExpert(e);
            }
            catch (Exception ex)
            {
                Messages.AddDebug(ex.Message);
                throw new Exception("Error creating expert");
            }
        }

        public static void createExpert(User u, Pathology pa, Method m, Plant p)
        {
            try
            {
                Expert e = new Expert();
                e.UserID = u.UserID;
                if (pa != null) e.PathologyID = pa.PathologyID;
                if (m != null) e.MethodID = m.MethodID;
                if (p != null) e.PlantID = p.PlantID;
                e.State = true;

                dataExpert.createExpert(e);
            }
            catch (Exception ex)
            {
                Messages.AddDebug(ex.Message);
                throw new Exception("Error creating expert");
            }
        }
        #endregion

        #region Get
        public static Boolean isExpertActive(Guid guid)
        {
            try
            {
                Expert e = dataExpert.getExpert(guid);

                return e.State;
            }
            catch (Exception ex)
            {
                Messages.AddDebug(ex.Message);
                throw new Exception("Error knowing if expert is active");
            }
        }

        public static Expert getExpert(Guid guid)
        {
            try
            {
                return dataExpert.getExpert(guid);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting expert");
            }
        }

        public static List<Expert> getExpertsByPathology(Pathology p)
        {
            try
            {
                Expression<Func<Expert, bool>> whereClause = (Expert e) => (e.PathologyID == p.PathologyID) && (e.State == true);

                return dataExpert.getExperts(whereClause).ToList();
            }
            catch (Exception ex)
            {
                Messages.AddDebug(ex.Message);
                throw new Exception("Error getting expert by pathology");
            }
        }

        public static List<Expert> getExpertsByMethod(Method m)
        {
            try
            {
                Expression<Func<Expert, bool>> whereClause = (Expert e) => (e.MethodID == m.MethodID) && (e.State == true);

                return dataExpert.getExperts(whereClause).ToList();
            }
            catch (Exception ex)
            {
                Messages.AddDebug(ex.Message);
                throw new Exception("Error getting expert by method");
            }
        }

        public static List<Expert> getExpertsByPlant(Plant p)
        {
            try
            {
                Expression<Func<Expert, bool>> whereClause = (Expert e) => (e.PlantID == p.PlantID) && (e.State == true);

                return dataExpert.getExperts(whereClause).ToList();
            }
            catch (Exception ex)
            {
                Messages.AddDebug(ex.Message);
                throw new Exception("Error getting expert by plant");
            }
        }
        #endregion

        #region Set
        public static Boolean changeExpert(Expert e)
        {
            try
            {
                return dataExpert.changeExpert(e);
            }
            catch (Exception ex)
            {
                Messages.AddDebug(ex.Message);
                throw new Exception("Error changing expert");
            }
        }

        public static Boolean activeExpert(Expert e)
        {
            try
            {
                e.State = true;
                return dataExpert.changeExpert(e);
            }
            catch (Exception ex)
            {
                Messages.AddDebug(ex.Message);
                throw new Exception("Error activing expert");
            }
        }

        public static Boolean inactiveExpert(Expert e)
        {
            try
            {
                e.State = false;
                return dataExpert.changeExpert(e);
            }
            catch (Exception ex)
            {
                Messages.AddDebug(ex.Message);
                throw new Exception("Error inactiving expert");
            }
        }
        #endregion

        #region Delete
        public static Boolean deleteExpert(Expert e)
        {
            try
            {
                return dataExpert.deleteExpert(e);
            }
            catch (Exception ex)
            {
                Messages.AddDebug(ex.Message);
                throw new Exception("Error deleting expert");
            }
        }
        #endregion
    }
}
